from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class SystemMessage(models.Model):
    """
    系统消息模型
    用于发布重要通知给用户
    """
    PRIORITY_CHOICES = [
        ('urgent', '紧急'),
        ('important', '重要'),
        ('normal', '一般'),
    ]
    
    title = models.CharField(
        max_length=200,
        verbose_name='消息标题',
        help_text='消息的标题'
    )
    
    content = models.TextField(
        verbose_name='消息内容',
        help_text='消息的详细内容'
    )
    
    priority = models.CharField(
        max_length=20,
        choices=PRIORITY_CHOICES,
        default='normal',
        verbose_name='优先级',
        help_text='消息的重要程度'
    )
    
    is_active = models.BooleanField(
        default=True,
        verbose_name='是否启用',
        help_text='消息是否激活显示'
    )
    
    target_roles = models.JSONField(
        default=list,
        blank=True,
        verbose_name='目标角色',
        help_text='消息的目标角色列表，空则表示全部角色'
    )
    
    target_companies = models.JSONField(
        default=list,
        blank=True,
        verbose_name='目标公司',
        help_text='消息的目标公司列表，空则表示全部公司'
    )
    
    created_by = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        verbose_name='创建者',
        help_text='创建该消息的用户'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间',
        help_text='消息创建的时间'
    )
    
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间',
        help_text='消息最后更新的时间'
    )
    
    expire_at = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name='过期时间',
        help_text='消息过期时间，为空则永不过期'
    )
    
    class Meta:
        verbose_name = '系统消息'
        verbose_name_plural = '系统消息'
        db_table = 'system_messages'
        ordering = ['-created_at']
    
    def __str__(self):
        return f'{self.get_priority_display()} - {self.title}'
    
    def is_expired(self):
        """
        检查消息是否已过期
        """
        if self.expire_at is None:
            return False
        from django.utils import timezone
        return timezone.now() > self.expire_at
    
    def get_read_count(self):
        """
        获取已读用户数量
        """
        return self.messagereadstatus_set.count()


class MessageReadStatus(models.Model):
    """
    消息阅读状态模型
    记录用户对消息的阅读状态
    """
    message = models.ForeignKey(
        SystemMessage,
        on_delete=models.CASCADE,
        verbose_name='消息',
        help_text='对应的系统消息'
    )
    
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        verbose_name='用户',
        help_text='阅读消息的用户'
    )
    
    read_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='阅读时间',
        help_text='用户阅读消息的时间'
    )
    
    class Meta:
        verbose_name = '消息阅读状态'
        verbose_name_plural = '消息阅读状态'
        db_table = 'message_read_status'
        unique_together = ('message', 'user')
        ordering = ['-read_at']
    
    def __str__(self):
        return f'{self.user.email} - {self.message.title}'
